package com.example.elm.Home_Page;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.elm.City_Name_JSON;
import com.example.elm.R;

import org.checkerframework.checker.units.qual.C;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

public class Choose_City_Page extends AppCompatActivity {

    LinearLayout set_district, choose_city, choose_provider;
    ProgressBar loading_data;
    TextView my_city_name;

    List<String> provider = new ArrayList<>();
    List<List<String>> city = new ArrayList<>();
    List<List<List<String>>> district = new ArrayList<>();
    View last_provider_view, last_city_view;
    String city_text = "";

    private class Loading_City_Thread extends Thread {
        @Override
        public void run() {
            try {
                String[] city_list = new City_Name_JSON().get_city();
                for (int i = 0; i < city_list.length; i++) {
                    String name_1 = city_list[i];
                    JSONObject object_2 = new JSONObject(name_1);
                    String name_2 = object_2.get("citys").toString();
                    provider.add(object_2.get("province").toString());
                    List<String> city_temp = new ArrayList<>();
                    List<List<String>> district_temp_1 = new ArrayList<>();
                    JSONArray object_3 = new JSONArray(name_2);
                    for (int j = 0; j < object_3.length(); j++) {
                        String name_3 = object_3.get(j).toString();
                        JSONObject object_4 = new JSONObject(name_3);
                        String city_name_4 = object_4.get("city").toString();
                        city_temp.add(city_name_4);
                        String area_name_4 = object_4.get("areas").toString();
                        JSONArray object_5 = new JSONArray(area_name_4);
                        List<String> district_temp_2 = new ArrayList<>();
                        for (int k = 0; k < object_5.length(); k++) {
                            String name_5 = object_5.get(k).toString();
                            JSONObject object_6 = new JSONObject(name_5);
                            String name_6 = object_6.get("area").toString();
                            district_temp_2.add(name_6);
                        }
                        district_temp_1.add(district_temp_2);
                    }
                    city.add(city_temp);
                    district.add(district_temp_1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            while (!provider.get(provider.size() - 1).equals("澳门特别行政区")) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    loading_data.setVisibility(View.GONE);
                    for (int i = 0; i < provider.size(); i++) {
                        View choose_provider_view = LayoutInflater.from(Choose_City_Page.this).inflate(R.layout.choose_city_provider_view, null);
                        LinearLayout choose_which = choose_provider_view.findViewById(R.id.choose_which);
                        TextView provider_text = choose_provider_view.findViewById(R.id.provider_text);
                        provider_text.setText(provider.get(i));
                        View provider_view = choose_provider_view.findViewById(R.id.provider_view);
                        if (i == 0) {
                            provider_view.setVisibility(View.VISIBLE);
                            View choose_city_view = LayoutInflater.from(Choose_City_Page.this).inflate(R.layout.choose_city_provider_view, null);
                            TextView city_text = choose_city_view.findViewById(R.id.provider_text);
                            city_text.setText(city.get(0).get(0));
                            View city_view = choose_city_view.findViewById(R.id.provider_view);
                            city_view.setVisibility(View.VISIBLE);
                            last_provider_view = provider_view;
                            last_city_view = city_view;

                            for (int k = 0; k < district.get(0).get(0).size(); k++) {
                                View set_district_view = LayoutInflater.from(Choose_City_Page.this).inflate(R.layout.set_city_view, null);
                                TextView district_name = set_district_view.findViewById(R.id.city_name);
                                district_name.setText(district.get(0).get(0).get(k));
                                LinearLayout choose_which_district = set_district_view.findViewById(R.id.choose_which_district);
                                int finalK = k;
                                choose_which_district.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Intent intent = new Intent(Choose_City_Page.this, Set_Location_Page.class);
                                        intent.putExtra("city_name", city.get(0).get(0));
                                        intent.putExtra("district_name", district.get(0).get(0).get(finalK));
                                        setResult(1, intent);
                                        finish();
                                    }
                                });
                                set_district.addView(set_district_view);
                            }
                            choose_city.addView(choose_city_view);
                        }
                        int finalI = i;
                        choose_which.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (provider_view != last_provider_view) {
                                    provider_view.setVisibility(View.VISIBLE);
                                    last_provider_view.setVisibility(View.GONE);
                                    last_provider_view = provider_view;
                                }
                                choose_city.removeAllViews();
                                for (int j = 0; j < city.get(finalI).size(); j++) {
                                    View choose_city_view = LayoutInflater.from(Choose_City_Page.this).inflate(R.layout.choose_city_provider_view, null);
                                    LinearLayout choose_which = choose_city_view.findViewById(R.id.choose_which);
                                    TextView provider_text = choose_city_view.findViewById(R.id.provider_text);
                                    provider_text.setText(city.get(finalI).get(j));
                                    View city_view = choose_city_view.findViewById(R.id.provider_view);
                                    if (j == 0) {
                                        if (city_view != last_city_view) {
                                            city_view.setVisibility(View.VISIBLE);
                                            last_city_view.setVisibility(View.GONE);
                                            last_city_view = city_view;
                                        }
                                        set_district.removeAllViews();
                                        for (int k = 0; k < district.get(finalI).get(0).size(); k++) {
                                            View set_district_view = LayoutInflater.from(Choose_City_Page.this).inflate(R.layout.set_city_view, null);
                                            TextView district_name = set_district_view.findViewById(R.id.city_name);
                                            district_name.setText(district.get(finalI).get(0).get(k));
                                            LinearLayout choose_which_district = set_district_view.findViewById(R.id.choose_which_district);
                                            int finalK = k;
                                            choose_which_district.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                    Intent intent = new Intent(Choose_City_Page.this, Set_Location_Page.class);
                                                    intent.putExtra("city_name", city.get(finalI).get(0));
                                                    intent.putExtra("district_name", district.get(finalI).get(0).get(finalK));
                                                    setResult(1, intent);
                                                    finish();
                                                }
                                            });
                                            set_district.addView(set_district_view);
                                        }
                                    }
                                    int finalJ = j;
                                    choose_which.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            if (city_view != last_city_view) {
                                                city_view.setVisibility(View.VISIBLE);
                                                last_city_view.setVisibility(View.GONE);
                                                last_city_view = city_view;
                                            }
                                            set_district.removeAllViews();
                                            for (int k = 0; k < district.get(finalI).get(finalJ).size(); k++) {
                                                View set_district_view = LayoutInflater.from(Choose_City_Page.this).inflate(R.layout.set_city_view, null);
                                                TextView district_name = set_district_view.findViewById(R.id.city_name);
                                                district_name.setText(district.get(finalI).get(finalJ).get(k));
                                                LinearLayout choose_which_district = set_district_view.findViewById(R.id.choose_which_district);
                                                int finalK = k;
                                                choose_which_district.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View view) {
                                                        Intent intent = new Intent(Choose_City_Page.this, Set_Location_Page.class);
                                                        intent.putExtra("city_name", city.get(finalI).get(finalJ));
                                                        intent.putExtra("district_name", district.get(finalI).get(finalJ).get(finalK));
                                                        setResult(1, intent);
                                                        finish();
                                                    }
                                                });
                                                set_district.addView(set_district_view);
                                            }
                                        }
                                    });
                                    choose_city.addView(choose_city_view);
                                }
                            }
                        });
                        choose_provider.addView(choose_provider_view);
                    }
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_city_page);

        city_text = getIntent().getStringExtra("city_text");

        set_district = findViewById(R.id.set_district);
        choose_city = findViewById(R.id.choose_city);
        choose_provider = findViewById(R.id.choose_provider);
        loading_data = findViewById(R.id.loading_data);
        my_city_name = findViewById(R.id.my_city_name);

        my_city_name.setText(city_text);

        Loading_City_Thread loadingCityThread = new Loading_City_Thread();
        loadingCityThread.start();
    }
}